<?php
/* @project : Template
 * @auther  : 青云
 * @date    : 2018/1/10
 * @desc    : 用户控制器
 */

namespace app\admin\controller;

use think\Request;
use app\common\model\Staff as StaffModel;
use app\common\model\Department as DepartmentModel;

class Staff extends Base
{
    /**
     * index
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function index(Request $request)
    {
        $params = input();
        $DepartmentModel = new DepartmentModel();
        $department_list  = $DepartmentModel->getDepartmentList();

        $user_list = [];
        // 如果Ajax 查询并返回数据
        if ($request->isAjax()) {
            $where_user = [
                ['a.is_del', 'eq', 0],
            ];

            // 关键词搜索
            if ($request->keyword) {
                $where_user[] = ['staff_no|username|nickname|real_name', 'like', '%' . $request->keyword . '%'];
            }

            // 部门搜索
            if ($request->dept_no) {
                $admin_id_list = $DepartmentModel->getStaffIdListByDeptId($request->dept_no);
                $where_user[]  = ['a.id', 'in', $admin_id_list];
            }

            $this->_search_filter($where_user);

            $StaffModel = (new StaffModel())->alias('a')
                ->join('department b', 'a.dept_no = b.id', 'LEFT')
                ->field('a.*, b.name as dept_name');
            $user_list     = $this->_list($StaffModel, $where_user, 'sort asc');

            return $user_list;
        }

        $data = [
            'user_list' => $user_list,
            'department_list' => $department_list,
        ];

        if (isset($params['is_debug'])) {
            halt($user_list);
        }

        $this->assign($data);
        return $this->fetch();
    }

    /**
     * add
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function add()
    {
        // 角色数据
        $DepartmentModel = new DepartmentModel();
        $department_list  = $DepartmentModel->getDepartmentList();
        $this->assign('department_list', $department_list);
        return $this->fetch();
    }
    /**
     * edit
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function edit(Request $request)
    {
        // 角色信息列表
        $DepartmentModel = new DepartmentModel();
        $department_list  = $DepartmentModel->getDepartmentList();
        $this->assign('department_list', $department_list);
        $vo = $this->_edit($request);
        return $this->fetch();
    }

    /**
     * del
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function del(Request $request)
    {
        $result = $this->_del($request);
        if ($result) {
            return $this->api_success('删除成功');
        }
        return $this->api_error('删除失败');
    }

    /**
     * save
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function save(Request $request)
    {
        $controller = $request->controller();
        $path = app()->getAppPath() . '/common/validate/' . $controller . '.php';
        if (file_exists($path)) {
            $validate = validate($controller);
            if (!$validate->check($request->param())) {
                $msg = $validate->getError();
                return $this->api_error($msg, $request->param());
            }
        }

        switch ($request->opmode) {
            case "add" :
                $result   = $this->_insert($request, $controller);
                $admin_id = $result->id;
                $msg      = '新增成功';
                break;
            case "edit" :
                $result   = $this->_update($request, $controller);
                $admin_id = $request->id;
                $msg      = '编辑成功';
                break;
            default :
                $msg    = '非法操作';
                $result = false;
                break;
        }

        if ($request->isAjax()) {
            if ($result != false) {
                return $this->api_success($msg, $result);
            }
            return $this->api_error($msg, $result);
        }
    }

    /**
     * 插入新新数据
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request    [description]
     * @param    [type]     $controller [description]
     * @return   [type]                 [description]
     */
    protected function _insert(Request $request, $controller = null)
    {
        if (empty($controller)) {
            $controller = $request->controller();
        }
        $data = $request->param();
        foreach ($data as $key => $item) {
            if (is_array($item)) {
                $data[$key] = implode('|', $item);
            }
        }
        $result = model($controller)->create($data, true);
        return $result;
    }

    /**
     * 更新数据
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request    [description]
     * @param    [type]     $controller [description]
     * @return   [type]                 [description]
     */
    protected function _update(Request $request, $controller = null)
    {
        if (empty($controller)) {
            $controller = $request->controller();
        }
        $data = $request->param();
        if (empty($data['password'])) {
            unset($data['password']);
        }
        foreach ($data as $key => $item) {
            if (is_array($item)) {
                $data[$key] = implode('|', $item);
            }
        }
        $result = model($controller)->data($data, true)->allowField(true)->force()->isUpdate(true)->save();
        return $result;
    }

    /**
     * sort_up
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function sort_up(Request $request)
    {
        return $this->_sort_up($request);
    }
    /**
     * sort_down
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function sort_down(Request $request)
    {
        return $this->_sort_down($request);
    }
    /**
     * set_valid
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function set_valid(Request $request)
    {
        return $this->_set_valid($request);
    }
    /**
     * set_invalid
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function set_invalid(Request $request)
    {
        return $this->_set_invalid($request);
    }
}